package com.wv.api.server.system;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.util.SaResult;
import com.wv.api.server.common.BaseController;
import com.wv.api.service.ISysUserService;
import com.wv.common.result.HttpCodeEnum;
import com.wv.common.result.PageResponseResult;
import com.wv.common.result.Result;
import com.wv.model.system.SysUser;
import com.wv.model.system.condition.SysUserCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "用户管理相关接口")
@RequestMapping("/user")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService iSysUserService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation(value = "获取用户列表")
    @SaCheckPermission("user:list")
    public Result get(@RequestBody SysUserCondition sysUserCondition){
        return iSysUserService.getUser(sysUserCondition);
    }

    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "添加用户")
    @SaCheckPermission("user:add")
    public Result add(@RequestBody @Validated SysUser sysUser){
        return iSysUserService.addUser(sysUser);
    }

    @RequestMapping(method = RequestMethod.PUT)
    @ApiOperation(value = "修改用户数据")
    @SaCheckPermission("user:update")
    public Result update(@RequestBody SysUser sysUser){
        return iSysUserService.updateUser(sysUser);
    }

    @RequestMapping(method = RequestMethod.DELETE)
    @ApiOperation(value = "删除用户")
    @SaCheckPermission("user:delete")
    public Result del(String id){
        return iSysUserService.deleteUser(id);
    }

    @RequestMapping(method = RequestMethod.PATCH)
    @ApiOperation(value = "重置用户密码")
    @SaCheckPermission("user:resetPwd")
    public Result resetPassword(SysUser sysUser){
        return iSysUserService.resetPassword(sysUser);
    }

    @GetMapping("/excel")
    @ApiOperation(value = "导出用户列表")
    public void exportExcel(HttpServletResponse response) {
        Result<List> userList = iSysUserService.getUserList();
        outExcel(response,userList.getData(),SysUser.class,"用户表格");
    }

    @PostMapping("/excel")
    @ApiOperation(value = "导入用户列表")
    public SaResult importExcel(@RequestPart("file")MultipartFile file) {
        List<SysUser> list = inputExcel(file, SysUser.class);
        iSysUserService.addUser(list);
        return success(HttpCodeEnum.SUCCESS);
    }
}
